package com.lqk.fnandroid.viewmodel

import android.util.Log
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.lqk.fnandroid.db.EntityUserBean
import com.lqk.fnandroid.net.FlaskAPI
import com.lqk.fnandroid.net.RetrofitHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainViewModel : ViewModel() {
    companion object {
        const val TAG = "MainViewModel"
    }

    var user: EntityUserBean? = null

    var api: FlaskAPI = RetrofitHelper.createService(FlaskAPI::class.java)

    var stateUrl = MutableLiveData("")

    private fun initRetrofit() {}

    fun doGet() {
        viewModelScope.launch(Dispatchers.IO) {
            val s = api.helloGet()
            Log.d(TAG, "doGet: $s")
            stateUrl.postValue(s.data)
        }
    }

    fun doPost() {
        val job = viewModelScope.launch(Dispatchers.IO) {
            async {
            }
            val s = api.helloPost()
            Log.d(TAG, "doPost: $s")
            stateUrl.postValue(s.data)
            delay(1000)
            val s1 = api.helloGet()
            Log.d(TAG, "doGet: $s1")
            stateUrl.postValue(s1.data)
        }
//        job.start()
    }
}